package com.moleq.mgpos.widget;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;

import com.moleq.base.AsyncImageLoader;
import com.moleq.base.AsyncImageLoader.ImageCallback;
import com.moleq.models.SaleListItem;
import com.moleq.mgpos.activities.R;
import com.moleq.service.ItemPicService;

public class FlipChartItemViewCache {
	private int mNumColumns;
	private View baseView;  
//    private TextView textView;
//    private TextView textView2;  
//    private ImageView imageView;
	private LayoutInflater mInflater;  
	
	private int mItemWidth = 130;
	private int mItemHeight = 130;
	private Context mContext;
	private int mOverRecordCount;
	private int mRowCount;
	private int mRecordCount;
	private OnTouchListener mItemOnTouchListener;
	private AsyncImageLoader imageLoader;
//	private ItemPicService mPicService;
	private int mfocuseRow;
	private int mfocuseCol;
	private int mRow;
	private int mCol;
	
	
	class ViewHolder   
    {  
        public ImageView localImageView = null;  
        public TextView localTextView1 = null;  
        public TextView localTextView2 = null;  
        public TextView localTextView3 = null;  
    }  

    public FlipChartItemViewCache(View baseView) {  
        this.baseView = baseView;  
    }  

    public FlipChartItemViewCache(View baseView, int cols) {
    	 this.baseView = baseView;
    	 mNumColumns = cols;
	}

	public FlipChartItemViewCache(Context context, int cols) {
		mContext = context;
		mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mNumColumns = cols;
//		mPicService = new ItemPicService(context, shareContext);
		imageLoader = new AsyncImageLoader(); 
//		imageLoader.setItemPicService(mPicService);
	}
	
	public void setItemOnTouchListener(OnTouchListener listener){
		mItemOnTouchListener  = listener;
	}
	
	private View buildColumnView(){
		View singleItemView ;
		singleItemView = mInflater.inflate(R.layout.flip_chart_item, null);
		RelativeLayout rl = (RelativeLayout)singleItemView.findViewById(R.id.flipChartItemContainer);
		LayoutParams lp = new LayoutParams(mItemWidth,mItemHeight);
		rl.setLayoutParams(lp);
		rl.setFocusable(true);
		rl.setFocusableInTouchMode(true);
		rl.setOnTouchListener(mItemOnTouchListener);
		return singleItemView;
	}

	public View getView() {
		
		int idx = -1;
		if(baseView == null){
			LinearLayout rowView = new LinearLayout(mContext);
			
			rowView.setFocusable(true);
			rowView.setFocusableInTouchMode(true);
			for(int i = 0 ; i < mNumColumns;i++){
				rowView.addView(buildColumnView());
			}
			baseView = rowView;
		}
		
		return baseView;
	}

	public void setValue(ArrayList<SaleListItem> items, int row,int focusedIdx) {
		int currentIdx = 0;
		
		mRecordCount = items.size();
		mRowCount = mRecordCount / mNumColumns + OverRow();
		mOverRecordCount = mRecordCount % mNumColumns;
		
		int k = mNumColumns;
		if(mOverRecordCount > 0 && row == mRowCount -1){
			k = mOverRecordCount;
		}
		int rowIdx = row * mNumColumns;
		
		View itemView;
		ViewHolder holder;
		LinearLayout rowView = (LinearLayout)baseView;
		for(int i = 0 ;i< mNumColumns;i ++){
			itemView = rowView.getChildAt(i);
			
			itemView.clearFocus();
			
		
			
			if (i< k){
				
				
				currentIdx = rowIdx + i;
				
				if(focusedIdx != -1 && focusedIdx == currentIdx){
					itemView.requestFocus();
				}
				
				holder = new ViewHolder();
				
				itemView.setVisibility(View.VISIBLE);
				
				if (items.get(currentIdx).getIsWeight()) {
					itemView.setBackgroundResource(R.drawable.bg_scalable_flip_chart_item_states);
				} else {
					itemView.setBackgroundResource(R.drawable.bg_regular_flip_chart_item_states);
				}
				
				holder.localImageView = (ImageView)itemView.findViewById(R.id.imageViewPic);
				holder.localTextView1 =  (TextView) itemView.findViewById(R.id.textViewDesc);
				holder.localTextView2 = (TextView) itemView.findViewById(R.id.textViewDesc2);
				
				//holder.localImageView.setImageDrawable(drawable);
				holder.localTextView1.setText(items.get(currentIdx).getName());
				holder.localTextView2.setText(items.get(currentIdx).getDescription());
				
				String imageUrl = items.get(currentIdx).getUpc();//imageAndText.getImageUrl();  
				final ImageView imageView = holder.localImageView;
				Drawable cachedImage = imageLoader.loadDrawable(imageUrl,new ImageCallback() {

					public void imageLoaded(Drawable imageDrawable,String imageUrl) {
//						
						if(imageDrawable == null){
//							imageView.setImageResource(R.drawable.default_logo_image_sm);
							imageView.setImageResource(R.drawable.default_image_sm);
						}else{
							imageView.setImageDrawable(imageDrawable);
						}
					}
				});
				
				if (cachedImage == null) {  
			           // imageView.setImageResource(R.drawable.default_image);  
		        }else{  
		            imageView.setImageDrawable(cachedImage);  
		        }  
				
				//itemView.setTag(items.get(currentIdx));
				itemView.setTag(currentIdx);
			}else{
				itemView.setVisibility(View.INVISIBLE);
			}
			
			
		
			
		}
		
	}  
	
	public int OverRow() {
		int pages = 0;
		if (mRecordCount % mNumColumns != 0)
			pages = 1;
		else
			pages = 0;
		return pages;
	}
	

}
